<?php
// error_reporting(E_ALL);

// ID: $Id: $

/**
 * User Class.
 *
 *
 */

define('USER_ANONYMOUS',     'NONNYMOUSE');

class User
{
    private $_wp_user;
    private $_user;

    /**
     * Class Initializer
     *
     */

    function __construct()
    {
        global $current_user;
        $this->_wp_user = $current_user;
        $this->init();
    }

    public function Dump()
    {
        var_dump($this->_user);
        var_dump($this->_wp_user);
    }

    private function ID() 
    {
        return $this->_wp_user->ID;
    }

    private function init()
    {
        global $db;

        if(! $this->is_logged_in()) {
            return;
        }
        // wp function
        $_id = $this->_wp_user->ID;
        $sql = "SELECT 
                    firstname,
                    middlename,
                    lastname,
                    namesuffix,
                    street1,
                    street2,
                    city,
                    state,
                    zip,
                    homephone,
                    phone2,
                    phone3,
                    phone4,
                    gender
                FROM bainbri2_tbicl.users
                WHERE wpid = {$_id}";
        $this->_user = $db->SqlOneObject($sql);
        if(empty($this->_user)) {
            $this->add_user();
        }
    }

    private function add_user() 
    {
        if(! $this->is_logged_in()) {
            return;
        }
        global $db;
        $sql = "INSERT INTO bainbri2_tbicl.users
                (
                    wpid,
                    username
                )
                VALUES
                (
                    {$this->_wp_user->ID},
                    '{$this->_wp_user->user_login}'
                )";
        var_dump($sql);
        $db->SqlExecute($sql);
    }

    private function is_logged_in() {
        return is_user_logged_in();
    }

    public function Email() {
        return empty( $obj->email )
                ? $this->_wp_user->email
                : $obj->email;
    }

    public function Username()
    {
        return $this->is_logged_in()
            ? USER_ANONYMOUS 
            : $this->_wpuser->user_login;
    }

    public function Firstname()
    {
        if($this->IsAnonymous())
        {
            return "I M";
        }
        else
        {
            return ! empty($this->_user)
                    && !empty($this->_user->firstname)
                ? $this->_user->firstname
                : $this->_wpuser->first_name;
        }
    }

    public function Lastname()
    {
        if($this->IsAnonymous())
        {
            return "Anonymous";
        }
        else
        {
            return ! empty($this->_user)
                    && !empty($this->_user->lastname)
                ? $this->_user->lastname
                : $this->_wpuser->last_name;
        }
    }

    public function Suffix()
    {
        if($this->IsAnonymous())
        {
            return "";
        }
        else
        {
            return $this->_user->namesuffix;
        }
    }

    public function IsAnonymous()
    {
        return empty($this->_wpuser);
    }
}

